using System;
using System.Data;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Reflection;
using System.Collections;
using StockTrader.DAL.HoseAdapter.Lib;
using StockTrader.DAL.HastcAdapter.Config;

namespace StockTrader.DAL.HastcAdapter.FileParser
{
    public class FileEngineer
    {
        public static ArrayList RetrieveFileInfo(Type objType)
        {
            ArrayList lstInfo = new ArrayList();
            DataSet ds = null;
            string srcFile; string desFile;

            if (objType != null)
            {
                string strXML = AppDomain.CurrentDomain.BaseDirectory.Replace("bin\\Debug\\", "") + "Config\\FileDir.xml";
                ds = new DataSet();
                ds.ReadXml(strXML);
                if (ds.Tables.Count > 0)
                {
                    DataView dv = ds.Tables["FileConfig"].DefaultView;
                    dv.RowFilter = "ClassName='" + objType.Name + "'";
                    if (dv.Count > 0)
                    {
                        srcFile = Settings.ROOT_STS + objType.Name + ".XML";
                        desFile = objType.Name + ".XML";

                        if (File.Exists(srcFile))
                        {
                            try
                            {
                                File.Delete(desFile);
                                CommonLib.SafeCopy(srcFile, desFile);
                            }
                            catch
                            {
                                return null;
                            }
                        }
                        if (File.Exists(desFile))
                        {
                            DataSet rds = new DataSet();
                            rds = ReadDataFile(desFile);

                            if (rds.Tables.Count > 0)
                            {
                                for (long i = 0; i < rds.Tables[0].Rows.Count; i++)
                                {
                                    Object objClass = Activator.CreateInstance(objType);
                                    if (null != objClass)
                                    {
                                        PropertyInfo[] lstProperties = objType.GetProperties();
                                        foreach (PropertyInfo objProperty in lstProperties)
                                        {
                                            Object objVal = null;
                                            #region Convertion
                                            if (rds.Tables[0].Rows[(int)i][objProperty.Name].GetType() != typeof(DBNull))
                                            {
                                                if (objProperty.PropertyType == typeof(string))
                                                    objVal = Convert.ToString(rds.Tables[0].Rows[(int)i][objProperty.Name]);
                                                else if (objProperty.PropertyType == typeof(Decimal))
                                                    objVal = Convert.ToDecimal(rds.Tables[0].Rows[(int)i][objProperty.Name]);
                                                else if (objProperty.PropertyType == typeof(DateTime))
                                                    objVal = Convert.ToDateTime(rds.Tables[0].Rows[(int)i][objProperty.Name]);
                                                else if (objProperty.PropertyType == typeof(Int16))
                                                    objVal = Convert.ToInt16(rds.Tables[0].Rows[(int)i][objProperty.Name]);
                                                else if (objProperty.PropertyType == typeof(Int32))
                                                    objVal = Convert.ToInt32(rds.Tables[0].Rows[(int)i][objProperty.Name]);
                                                else if (objProperty.PropertyType == typeof(Int64))
                                                    objVal = Convert.ToInt64(rds.Tables[0].Rows[(int)i][objProperty.Name]);
                                                else if (objProperty.PropertyType == typeof(Double))
                                                    objVal = Convert.ToDouble(rds.Tables[0].Rows[(int)i][objProperty.Name]);
                                                else
                                                    objVal = rds.Tables[0].Rows[(int)i][objProperty.Name];

                                                objProperty.SetValue(objClass, objVal, null);
                                            }
                                            #endregion
                                        }
                                    }
                                    lstInfo.Add(objClass);
                                }
                            }
                        }
                    }
                }
            }
            return lstInfo;
        }
        private static DataSet ReadDataFile(string file)
        {
            DataSet ds = new DataSet();
            FileStream fsReadXml = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            try
            {
                ds.ReadXml(fsReadXml);
                return ds;
            }
            catch (Exception ex)
            {
                return ds;
            }
            finally
            {
                fsReadXml.Close();                
            }
        }
        public sealed class STS_FILES
        {
            public const string STOCKINFO = "STS_STOCKSINFO";
            public const string MARKETINFO = "STS_MARKETINFO";
            public const string BEST3A = "STS_BEST3_PRICE_A";
            public const string BEST3B = "STS_BEST3_PRICE_B";
            public const string ORDERS = "STS_ORDERS";
            public const string TRADINGRESULT = "STS_TRADING_RESULT";
        }
    }
}

